Ext.define('Module.System.UserMenu', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.menumanager',
//	border: false,
	layout: 'border',
	initComponent : function() {
		var me = this;
		
		var userMuenStore=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['userid','menuid','text','sort'],
	        proxy: {
	            type: 'ajax',
	            url: 'menu/getUserMenu.do'
	        }
		});

		var menutree = me.menutree =  Ext.create('Ext.tree.Panel',{
			title: '菜单列表',
			rootVisible: false,
			border: false,
            trackMouseOver:true,
			width: 450,
			dataIndex:'checked',
			region: 'west',
			tbar: ['->',{
				text: '移入',
				iconCls: 'icon-toend',
				handler: function(){
					var rec = menutree.getSelectionModel().getSelection()[0];

					if(rec && rec.data.id>0){
						me.addUserMenu(rec);
					}
					else{
						Ext.Msg.alert('提示', '请选择要移入的菜单。');
					}
				}
			}],
			store: Ext.create('Ext.data.TreeStore', {
				fields: ['id','index'/*,'leaf'*/,'link','pid','text','linktype','defaultshow'],
		        proxy: {
		            type: 'ajax',
		            url: 'menu/getMenu2.do'
		        },
		        checked:false,
		        root: {
		            id: '0',
		            text: '根目录(ROOT)',
		            expanded: true,
		            children: me.getMenuFolder()	
		        }
		    }),
			columns: [{
				xtype: 'treecolumn', 
	            text: '资源名称',
	            flex: 2,
	            sortable: false,
	            dataIndex: 'text'
			},{
	            text: '类型',
	            flex: 1,
	            sortable: false,
	            renderer: function(v,met,rec){
	            	switch (rec.raw.leaf) {
					case 0: return '目录';
					case 1: return '资源';
					default: return '目录';
					}
	            }
			}]
		});
		
		var symm = me.symm= Ext.create('Ext.grid.Panel',{
			title: '首页菜单设置',
			region: 'center',
			tbar: ['从菜单拖动菜单到本页面'],
			store: userMuenStore,
			selModel: Ext.create('Ext.selection.CheckboxModel',{
				mode:'SIMPLE'
			}),
			tbar: [{
				text: '删除',
				iconCls: 'icon-delete',
				handler: function(){
					var rec = symm.getSelectionModel().getSelection();
					if(rec.length !=0){
						me.deleteUserMenu(rec);
					}
					else{
						Ext.Msg.alert('提示', '请选择要删除的菜单。');
					}
				}
			}],
		    columns: [
		        { text: '菜单名称', dataIndex: 'text', sortable: false,width: 220},
		        { text: '显示顺序', dataIndex:'sort', sortable: false,width: 80}
		    ],
			viewConfig: {  
	            plugins: {  
	                ptype: "gridviewdragdrop",  
	                ddGroup:'DragDropGroup',
	                dragText: "可用鼠标拖拽进行上下排序"  
	            },  
	            listeners: {
		            drop: function(node, data, dropRec, dropPosition) {
		                //拖拽的时候刷新列表即可
		            	me.paixu();
	                }
	            }  
	        }
		});
		
		me.defaults = {padding: 1};
		me.items = [menutree,symm];
		
		
		me.callParent();
	},
	addUserMenu: function(rec){
		var me = this;
		Ext.Ajax.request({
		    url: 'menu/addUserMenu.do',
		    params:{menuid :rec.data.id} ,
		    success: function(response){
    	    	var rs = Ext.decode(response.responseText);
    	    	if(rs.rst){
    	    		me.symm.store.reload();
    	    	}
    	    	else{
    	    		Ext.Msg.alert('提示',rs.msg);
    	    	}
		    }
		});
	},
	deleteUserMenu: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除吗？',function(b){
				if('yes' == b){
					var menuidArr=[];
					for ( var i = 0; i < rec.length; i++) {
						menuidArr.push(rec[i].get('menuid'));
					}
					Ext.Ajax.request({
					    url: 'menu/deleteUserMenu.do',
					    params: {menuidArr : menuidArr},
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	me.symm.store.remove(rec);
        	    	    	me.paixu();
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	getMenuFolder: function(){
		return [{id: -1, text: "系统管理", leaf: false},
                {id: -2, text: "基础信息", leaf: false},
                {id: -3, text: "任务管理", leaf: false},
                {id: -4, text: "视频管理", leaf: false},
                {id: -5, text: "违规管理", leaf: false},
                {id: -6, text: "数据分析", leaf: false}];
	},
	/**
	 * 排序
	 */
	paixu : function(){
		var me=this;
		var store=me.symm.store;
    	
    	var idArr=[];
        for ( var i = 0; i < store.getCount(); i++) {
        	 idArr.push(store.getAt(i).data.menuid);
		}
         
     	Ext.Ajax.request({
		    url: 'menu/updateUserMenuSort.do',
		    params: {
		    	idArr:idArr.join(',')
	    	},
	    	success: function(response){
	    		me.symm.store.reload();
			}
		});
	}
});